package com.fresh.controller;

import com.fresh.common.ResponseResult;
import com.github.pagehelper.PageInfo;
import com.fresh.entity.RejIn;
import com.fresh.entity.RejInQO;
import com.fresh.service.CiPinService;
import com.fresh.util.SnowflakeIdWorker;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/cipin")
public class CiPinController {
    @Autowired
    SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    CiPinService ciPinService;

    @GetMapping({"/list/{pageNum}"})
    public Object list(RejInQO rejInQO, @PathVariable(required = false) Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<RejIn> pageInfo = ciPinService.findPageInfoByCondition(rejInQO, pageNum, 4);
        return new ResponseResult(200, "success", pageInfo);

    }

    @GlobalTransactional(name = "fsp-create-add", rollbackFor = Exception.class)
    @PostMapping("/add")
    public Object add(@RequestBody RejIn rejIn) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = sdf.format(dt);

        rejIn.setRejId(snowflakeIdWorker.nextId());
        rejIn.setRejIn(data);
        ciPinService.add(rejIn);
        return new ResponseResult(200, "success", null);
    }

}
